/*
* SpecificHeatController.java
* Created on  202015/6/26 10:23
* �汾       �޸�ʱ��          ����      �޸�����
* V1.0.1    202015/6/26       longshihui    ��ʼ�汾
*
*/

package com.infitecs.eyas.enthalpy;

import com.infitecs.eyas.enthalpy.model.Smoke;
import com.infitecs.eyas.enthalpy.model.SpecificHeat;
import com.infitecs.eyas.enthalpy.service.SpecificHeatService;
import com.infitecs.eyas.session.model.Param;
import com.infitecs.krill.ajax.response.AjaxResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 *
 * @author longshihui
 * @version 1.0.1
 */
@Controller
@RequestMapping("/enthalpy/specific_heat")
public class SpecificHeatController {
    @Autowired
    SpecificHeatService specificHeatService;

    @RequestMapping("")
    public String getPage(HttpSession session){
        try {
            int caseId = Integer.parseInt(session.getAttribute("caseId").toString());
            if (specificHeatService.getSpecificHeat(caseId) == null) {
                specificHeatService.addSpecificHeat(caseId);
            }
        } catch (Exception e) {
            return "enthalpy/specific_heat";
        }
        return "enthalpy/specific_heat";
    }

    @RequestMapping(value = "/update/{Kx}", method = RequestMethod.POST)
    @ResponseBody
    public void updateGx(@PathVariable("Kx") String name, HttpServletRequest request, HttpSession session) {
        int caseId = Integer.parseInt(session.getAttribute("caseId").toString());
        Param param = new Param(name.trim());
        param.setParamValue(Double.parseDouble(request.getParameter(name)));
        param.setCaseId(caseId);
        specificHeatService.updateKx(param);
    }

    @RequestMapping(value = "/getSpecificHeat", method = RequestMethod.POST)
    @ResponseBody
    public SpecificHeat getSpecificHeat(HttpSession session) {
        int caseId = Integer.parseInt(session.getAttribute("caseId").toString());
        return specificHeatService.getSpecificHeat(caseId);
    }

    @RequestMapping(value = "/calculateAll", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResponse calAll(HttpSession session) {
        int caseId = Integer.parseInt(session.getAttribute("caseId").toString());
        Map<String,Param> map = new HashMap<>();
        map.put("K5",new Param("K5"));
        map.put("K6",new Param("K6"));
        map.put("K7",new Param("K7"));
        map.put("K8",new Param("K8"));
        for (Param param: map.values()){
            param.setCaseId(caseId);
        }
        AjaxResponse response = new AjaxResponse();
        response.addAttribute("K5",specificHeatService.calK5(map.get("K5")));
        response.addAttribute("K6",specificHeatService.calK6(map.get("K6")));
        response.addAttribute("K7",specificHeatService.calK7(map.get("K7")));
        response.addAttribute("K8",specificHeatService.calK8(map.get("K8")));
        return response;
    }

}
